//
//  WBTextView.swift
//  0829-Weibo
//
//  Created by 毕仲阳 on 16/9/8.
//  Copyright © 2016年 mycompany. All rights reserved.
//

import UIKit

class WBTextView: UITextView {
    var placeHolderText:String?{
        didSet{
            placeHolderLabel.text = placeHolderText
        }
    }
    override var font: UIFont?{
        didSet{
            placeHolderLabel.font = font
        }
    }
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        self.setUI()
        NotificationCenter.default.addObserver(self, selector: #selector(textDidChange), name: NSNotification.Name.UITextViewTextDidChange, object: nil)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    @objc private func textDidChange(){
        placeHolderLabel.isHidden = hasText
    }
    private func setUI()
    {
        self.addSubview(placeHolderLabel)
        placeHolderLabel.snp_makeConstraints { (make)->Void in
            make.top.equalTo(self).offset(8)
            make.left.equalTo(self).offset(5)
        }
    }
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    lazy var placeHolderLabel : UILabel = {
        let label = UILabel(textColor: UIColor.lightGray, fontSize: 12)
        //label.isUserInteractionEnabled = true
        label.numberOfLines = 0
        return label
    }()
}
